package io.renren.modules.sys.controller;

import java.util.Arrays;
import java.util.Map;

import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.UpdateGroup;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.sys.entity.JxLaboratoryEntity;
import io.renren.modules.sys.service.JxLaboratoryService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 实验室
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2021-10-08 15:21:40
 */
@RestController
@RequestMapping("sys/jxlaboratory")
public class JxLaboratoryController {
    @Autowired
    private JxLaboratoryService jxLaboratoryService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:jxlaboratory:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = jxLaboratoryService.getAllWithPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:jxlaboratory:info")
    public R info(@PathVariable("id") Long id){
        JxLaboratoryEntity jxLaboratory = jxLaboratoryService.getById(id);

        return R.ok().put("jxLaboratory", jxLaboratory);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:jxlaboratory:save")
    public R save(@RequestParam Map<String, Object> params){
        JxLaboratoryEntity jxLaboratory=new JxLaboratoryEntity();
        jxLaboratory.setName(null!=params.get("name")?params.get("name").toString():null);
        jxLaboratory.setIntroduction(null!=params.get("introduction")?params.get("introduction").toString():null);
        jxLaboratory.setLaboratoryDesc(null!=params.get("laboratoryDesc")?params.get("laboratoryDesc").toString():null);
        jxLaboratory.setNum(null!=params.get("num")?params.get("num").toString():null);
        jxLaboratory.setOrderNum(null!=params.get("orderNum")?Integer.parseInt(params.get("orderNum").toString()):null);
        jxLaboratory.setSchoolId(null!=params.get("schoolId")?Long.valueOf(params.get("schoolId").toString()):null);
        jxLaboratory.setImgUrl(null!=params.get("imgUrl")?params.get("imgUrl").toString():null);
        jxLaboratory.setInternetIp(null!=params.get("internetIp")?params.get("internetIp").toString():null);
        jxLaboratory.setDeviceSize(null!=params.get("deviceSize")?Integer.parseInt(params.get("deviceSize").toString()):null);
        jxLaboratory.setDeviceSize(0);
        ValidatorUtils.validateEntity(jxLaboratory, AddGroup.class);
        jxLaboratoryService.save(jxLaboratory);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:jxlaboratory:update")
    public R update(@RequestParam Map<String, Object> params){
        JxLaboratoryEntity jxLaboratory=new JxLaboratoryEntity();
        jxLaboratory.setId(null!=params.get("id")?Long.parseLong(params.get("id").toString()):null);
        jxLaboratory.setName(null!=params.get("name")?params.get("name").toString():null);
        jxLaboratory.setIntroduction(null!=params.get("introduction")?params.get("introduction").toString():null);
        jxLaboratory.setLaboratoryDesc(null!=params.get("laboratoryDesc")?params.get("laboratoryDesc").toString():null);
        jxLaboratory.setNum(null!=params.get("num")?params.get("num").toString():null);
        jxLaboratory.setOrderNum(null!=params.get("orderNum")?Integer.parseInt(params.get("orderNum").toString()):null);
        jxLaboratory.setSchoolId(null!=params.get("schoolId")?Long.valueOf(params.get("schoolId").toString()):null);
        jxLaboratory.setImgUrl(null!=params.get("imgUrl")?params.get("imgUrl").toString():null);
        jxLaboratory.setInternetIp(null!=params.get("internetIp")?params.get("internetIp").toString():null);
        jxLaboratory.setDeviceSize(null!=params.get("deviceSize")?Integer.parseInt(params.get("deviceSize").toString()):null);
        ValidatorUtils.validateEntity(jxLaboratory,UpdateGroup.class);
        jxLaboratoryService.updateById(jxLaboratory);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:jxlaboratory:delete")
    public R delete(@RequestBody Long[] ids){
        jxLaboratoryService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /** 列表
     */
    @RequestMapping("/allList")
    public R AllList(){
        return R.ok().put("list", jxLaboratoryService.getAllLaboratory());
    }

}
